package dao

import (
	"zg3zhoukao/initialize"
	"zg3zhoukao/models"
)

func ArticleAdd(userId int, title string, text string) (article *models.Article, err error) {
	article = &models.Article{
		UserId: userId,
		Title:  title,
		Text:   text,
	}
	err = initialize.DB.Create(article).Error
	if err != nil {
		return nil, err
	}
	return
}

func ArticleHistory(userId int, articleId int) (article *models.Article_History, err error) {
	article = &models.Article_History{
		UserId:    userId,
		ArticleId: articleId,
	}
	err = initialize.DB.Create(article).Error
	if err != nil {
		return nil, err
	}
	return
}

func ArticleGood(userId int, articleId int) (article *models.Article_Good, err error) {
	article = &models.Article_Good{
		UserId:    userId,
		ArticleId: articleId,
	}
	err = initialize.DB.Create(article).Error
	if err != nil {
		return nil, err
	}
	return
}

func ReviewAdd(userId int, articleId int, conText string) (review *models.Article_Review, err error) {
	review = &models.Article_Review{
		UserId:    userId,
		ArticleId: articleId,
		Context:   conText,
	}
	err = initialize.DB.Create(review).Error
	if err != nil {
		return nil, err
	}
	return
}

func ReviewAddAdd(userId int, articleId int, fatherId int, conText string) (review *models.Article_Review, err error) {
	review = &models.Article_Review{
		UserId:    userId,
		ArticleId: articleId,
		FatherId:  fatherId,
		Context:   conText,
	}
	err = initialize.DB.Create(review).Error
	if err != nil {
		return nil, err
	}
	return
}

func ReviewGood(userId int, reviewId int) (review *models.Review_Good, err error) {
	review = &models.Review_Good{
		UserId:   userId,
		ReviewId: reviewId,
	}
	err = initialize.DB.Create(review).Error
	if err != nil {
		return nil, err
	}
	return
}

func ArticleList(title string) (article []*models.Article, err error) {
	if title == "" {
		initialize.DB.Find(&article)
		return
	} else {
		initialize.DB.Where("title=?", title).Find(&article)
		return
	}
}

func ReviewDel(id int) (review *models.Article_Review, err error) {
	initialize.DB.Raw("SELECT * FROM article_review Delete id=?", id).Scan(review)
	return nil, err
}
